local desc_wjsl_mode = [[
  # 巅峰神塔简介
  ___

  人数：2~8人，最多3人玩家组队（需要游戏人数大于4），当游戏人数2人时为单人爬塔模式。否则需要抵抗多敌人人机。

  胜利条件：玩家通关第42层(多人为49层)。

  失败条件：玩家全部死亡或者投降。玩家自由选将不在白名单列表。敌对人机总数小于等于玩家总数。

  层主技能（有“层数：X”的人机主公，X为层数）：1.随机技能：每次层主复活会获得额外随机技能。2.关卡技能：层主会在固定层数获得永久不变的技能

  神将层主：实力远高于层主，击败后获得其神将身上任意一个技能。

  卡牌：无视开启的扩展卡组。会自动过滤其他卡组，只留下标准包，军争包，浪包。去除诸葛连弩，延时锦囊牌

  规则：冲塔层数，击败神将。到达神塔巅峰42层(多人为49层)。每层对抗白名单将池内随机的不重复武将。当每次击杀层主，塔层层数+1，复活后重置武将并摸等量的牌；层主根据层数获得随机技能、最大体力、初始手牌；强度呈线性增长

  玩家击败层主后会根据不同层数获得选项增益。增益包括【随机的永久技能】，【随机的最大体力值上限】，【随机的金币】，【魔盒】、【杀伤】、【杀数】、【锦伤】标记属性加成，【守护天使】（复活甲设定）等。

]]

--是否开启写入武将白名单
local ISwrite = false
--开启测试
local IStest = false
--玩家测试武将
local TESTgen = "mouxusheng"
--测试敌人AI武将（避免自由选）
local TESTgens = {"qinghegongzhu","ty__huaxin","ty_ex__guohuai","m_ex__bulianshi","ty_ex__sunluban","ty_ex__chengpu","mou__xiahoushi","ty_ex__wuguotai","ty_ex__fazheng","chengong","xielingyu","zhouyi","ol__tengfanglan","dongyun","l__zhangliao","ex__simayi","ol_ex__zhangjiao","ty_ex__zhuran","l__lvbu","ty__yanghu","zhaotongzhaoguang","joy__xunyou","miniex__huangyueying","ty__yanghu","joy__xunyou","godzhaoyun","js__guanyu","lvlingqi","dukui","ty_ex__wuyi","ol_ex__sunjian","xizhicai","qinmi","godganning","ty__chenlin","ex__ganning","yuejiu","caomao","ty__lidian","ol__luyusheng","godganning","ty_ex__caozhi","zhaotongzhaoguang"}
--精选热门武将不必自由选
local HOTgenerals = {"js__liubei","qinmi","ty__caochun","ol__yanghu","l__zhangliao","m_ex__xusheng","ol__huangzu","hulao2__godlvbu","mou__machao","os_ex__guohuai","liyixiejing"}
--过滤其他卡组（只允许白名单的卡组加入牌堆）
local WHILEcards = {"standard","standard_cards","maneuvering","lang_cards","tenyear_token","mobile_derived","ol_token","overseas_token"}


local EndLessPractice = require "packages/wdtx/EndLessPractice"
local WUltiity = require "packages/wdtx/WUltiity"

local wjsl_getLogic = function()
  local wjsl_logic = GameLogic:subclass("wjsl_logic")

  function wjsl_logic:initialize(room)
    GameLogic.initialize(self, room)
    self.role_table = {
      { "lord" },
      { "lord", "rebel" },
      { "lord", "rebel", "rebel" },
      { "lord", "rebel", "rebel", "rebel" },
      { "lord", "rebel", "rebel", "rebel", "rebel" },
      { "lord", "loyalist", "loyalist", "loyalist", "rebel", "rebel" },
      { "lord", "rebel", "rebel", "rebel", "rebel", "rebel", "rebel" },
      { "lord", "loyalist", "loyalist", "rebel", "rebel", "rebel", "loyalist", "loyalist" },
    }
  end

  function wjsl_logic:prepareForStart()
    local room = self.room
    local players = room.players
    self:addTriggerSkill(Fk.skills["game_rule"] --[[@as TriggerSkill]])
    for _, trig in ipairs(Fk.global_trigger) do
      if trig:getSkeleton().name ~= "mobile_effect" then  --关闭手杀特效
        self:addTriggerSkill(trig)
      end
    end
    -- for _, trig in ipairs(Fk.legacy_global_trigger) do
    --   self:addTriggerSkill(trig)
    -- end
    self.room:sendLog{ type = "$GameStart" }
    self.room:sendLog{ type = "#wdtx_gamestart" , toast = true }
    self.room:setBanner("@[:]wjsl_mode-intro","wjsl-intro")
  end

  -- function wjsl_logic:prepareDrawPile()
  --   local room = self.room
  --   --重新过滤卡组
  --   local filterCardIds = {}
  --   local allCardIds = Fk:getAllCardIds()
  --   -- local excepts = {"crossbow","indulgence","supply_shortage","lightning"} -- 删除一些卡组
  --   local excepts = {"lightning"} -- 删除一些卡组
  --   for i = 1, #allCardIds do
  --     local id = allCardIds[i]
  --     local card = Fk:getCardById(id)
  --     if card then
  --       if card.is_derived then
  --         table.insert(room.void, id)
  --         room:setCardArea(id, Card.Void, nil)
  --       else
  --         if table.contains(WHILEcards,card.package.name) then
  --           if not table.contains(excepts,card.trueName) then
  --             table.insert(filterCardIds,id)
  --           end
  --         end
  --       end
  --     end
  --   end
  --   table.shuffle(filterCardIds)
  --   room.draw_pile = filterCardIds
  --   for _, id in ipairs(room.draw_pile) do
  --     room:setCardArea(id, Card.DrawPile, nil)
  --   end
  -- end

  function wjsl_logic:assignRoles()
    local room = self.room
    local n = #room.players
    local roles = self.role_table[n]
    table.shuffle(roles)
    local bots_t = {}
    local players_t = {}
    for i = 1, n do
      local p = room.players[i]
      local playerInfo = {id = p.id, index = i}
      if p.id < 0 then
        table.insert(bots_t, playerInfo)
      else
        table.insert(players_t, playerInfo)
      end
    end
    local botsrand_t = table.random(bots_t)
    local bot_player = room.players[botsrand_t.index]
    bot_player.role = "lord"
    bot_player.role_shown = true
    room:broadcastProperty(bot_player, "role")
    local loyalists = (n == 5 or n == 6) and 3 or 4
    local loyalistnum = 0
    for i = 1, n do
      local p = room.players[i]
      if p.role ~= "lord" then
        if p.id < 0 then
          local role = loyalistnum < loyalists and "loyalist" or "rebel"
          p.role = role
          loyalistnum = loyalistnum + 1
          if IStest then
            -- p.role ="rebel"
            p.role = i % 2 == 1 and "rebel" or "loyalist"
          end
        else
          p.role = "rebel"
        end
        room:broadcastProperty(p, "role")
        room:setPlayerProperty(p, "role_shown", true)
      end
    end
  end

  function wjsl_logic:chooseGenerals()
    local room = self.room
    local n = room:getSettings("enableDeputy") and 2 or 1
    local generalNum = room:getSettings("generalNum")
    local lord = room:getLord()
    if not lord then
      lord = room.players[1]
    end
    room:setCurrent(lord)
    local a1 = #room.general_pile
    local a2 = #room.players * generalNum
    if a1 < a2 then
      room:sendLog{
        type = "#NoEnoughGeneralDraw",
        arg = a1,
        arg2 = a2,
        toast = true,
      }
      room:gameOver("")
    end

    local nonlord = room.players
    local noai = table.filter(room.players, function(p) return p.id > 0 end)
    local seats = {}
    for _, p in ipairs(noai) do
      table.insert(seats, p.seat)
    end
    table.sort(seats, function(a, b) return a > b end)
    --获取可换的武将，人机就不换将了，座位靠后的先得换将卡
    local function getChange(all_cards, can_change, id)
      if not table.contains(noai, room:getPlayerById(id)) then
        return {}
      end
      local seat = room:getPlayerById(id).seat
      local change = {}
      local index = 0
      for i, s in ipairs(seats) do
        if s == seat then
          index = i
          break
        end
      end
      local start_index = (index - 1) * can_change + 1
      local end_index = index * can_change + 1
      change = table.slice(all_cards, start_index, end_index)
      return change
    end

    self.endlPrac = EndLessPractice:new()

    local can_change = room:getBanner("changeGeneralNum") or room:getSettings("generalNum") --换将次数
    local generals = room:getNGenerals(generalNum * #room.players)
    local all_cards = table.map(WUltiity.getGenerals( (can_change * #noai), room.general_pile, generals), Util.NameMapper)
    table.shuffle(all_cards)

    local data = {}
    for i, p in ipairs(nonlord) do
      local start_index = (i - 1) * generalNum + 1
      local end_index = i * generalNum + 1
      local arg = table.slice(generals, start_index, end_index)
      data[p.id] = {
        change = getChange(all_cards, can_change, p.id),
        cards = arg,  --generals
        num = n,  --n
        no_c = false, --no_convert
        heg = false, --heg
        type = "askForGeneralsChosen", --rule
        extra = {n = n}, --extra_data
      }
    end

    local req = room:askToMiniGame(nonlord,{
      skill_name = "AskForGeneral",
      game_type = "ChooseGeneralWithCanChangeForWDTX",
      data_table = data,
      timeout = room:getSettings("generalTimeout"),
    })

    for _, p in ipairs(nonlord) do
      local result = req:getResult(p)
      local general = result[1]
      local deputy = result[2]
      room:prepareGeneral(p, general, deputy)
    end

    room:askToChooseKingdom(nonlord)
    for _, p in ipairs(nonlord) do
      room:broadcastProperty(p, "general")
      room:broadcastProperty(p, "deputyGeneral")
      if p.role == "lord" then
        room:broadcastProperty(p, "kingdom")
      end
    end
  end
  return wjsl_logic
end

local wjsl_mode = fk.CreateGameMode{
  name = "wjsl_mode",
  minPlayer = 2,
  maxPlayer = 8,
  rule = "#wjsl_rule",
  logic = wjsl_getLogic,
  build_draw_pile = function(self)
    local filterCardIds = {}
    local void = {}
    local allCardIds = Fk:getAllCardIds()
    local excepts = {"lightning"} -- 删除一些卡组
    for i = 1, #allCardIds do
      local id = allCardIds[i]
      local card = Fk:getCardById(id)
      if card then
        if card.is_derived then
          -- table.removeOne(allCardIds, id)
          table.insert(void, id)
        else
          -- if table.contains(WHILEcards,card.package.name) then
            if not table.contains(excepts,card.trueName) then
              table.insert(filterCardIds,id)
            end
          -- end
        end
      end
    end
    return filterCardIds, void
  end,
  winner_getter = function(self, victim)
    if not victim.surrendered and victim.rest > 0 then
      return ""
    end
    local room = victim.room
    local alive = table.filter(room.players, function(p) ---@type Player[]
      return not p.surrendered and not (p.dead and p.rest == 0)
    end)
    if #alive == 0 then
      return ""
    end
    local winner = alive[1].role
    for _, p in ipairs(alive) do
      if p.role ~= winner then
        return ""
      end
    end
    return winner
  end,
  surrender_func = function(self, playedTime)
    return { { text = "你可以随时投降（组队时要征得队友同意哦）。", passed = true } }
  end,
}

Fk:loadTranslationTable{
  ["@[:]wjsl_mode-intro"] = "模式",
  ["wjsl-intro"] = "巅峰神塔(点击查看)",
  [":wjsl-intro"] = "<br>人数：2~8人，最多3人玩家组队（需要游戏人数大于4），当游戏人数2人时为单人爬塔模式。否则需要抵抗多敌人人机。<br>\
  <br>胜利条件：玩家通关第42层(多人为49层)。<br>\
  <br>失败条件：玩家全部死亡或者投降。玩家自由选将不在白名单列表。敌对人机总数小于等于玩家总数。<br>\
  <br>层主技能（有“层数：X”的人机主公，X为层数）：<br>1.随机技能：每次层主复活会获得额外随机技能。<br>2.关卡技能：层主会在固定层数获得永久不变的技能<br>\
  <br>神将层主：实力远高于层主，击败后获得其神将身上任意一个技能。<br>\
  <br>卡牌：无视开启的扩展卡组。会自动过滤其他卡组，只留下标准包，军争包，浪包。去除诸葛连弩，延时锦囊牌<br>\
  <br>规则：冲塔层数，击败神将。到达神塔巅峰42层(多人为49层)。<br>每层对抗白名单将池内随机的不重复武将。当每次击杀层主，塔层层数+1，复活后重置武将并摸等量的牌；层主根据层数获得随机技能、最大体力、初始手牌；强度呈线性增长<br>\
  <br>玩家击败层主后会根据不同层数获得选项增益。增益包括【随机的永久技能】，【随机的最大体力值上限】，【随机的金币】，【魔盒】、【杀伤】、【杀数】、【锦伤】标记属性加成，【守护天使】（复活甲设定）等。",
  ["wjsl_mode"] = "巅峰神塔",
  ["#wjsl_rule"] = "巅峰神塔",
  ["@wj_layer"] = "层数:",

  ["wjsl_zhenta"] = "镇塔",
  [":wjsl_zhenta"] = "锁定技，每名其他角色回合开始时，使用牌会计入次数，计数为最大体力值的两倍（队友忠臣人机是3倍，最多15）（当玩家最大体力值超过5，计数只+1且最多为15），使用卡牌结算后或者因【铸甲】获得护甲，计数-1。当计数为0时结束当前阶段。",

  ["wjsl_zhujia"] = "铸甲",
  [":wjsl_zhujia"] = "出牌阶段限制（层数/10|向上取整）* 2次。你可以重铸1张【闪】或者【装备】牌，也可以丢弃两张【闪】或【装备】牌获得1枚护甲（最多为5护甲），该护甲在你下回合开始时如果还存在则失去先前获得数量",

  ["wjsl_tazhen"] = "塔震",
  [":wjsl_tazhen"] = "锁定技，当你受到其他角色大于1伤害后，你令伤害来源弃随机X张牌并受到X点伤害（X为受到伤害值）",

  ["df_fengkuang"] = "疯狂",
  [":df_fengkuang"] = "锁定技，当你体力为最大体力一半以及一半以下，你无视被禁用卡与使用对目标的卡被禁技能规则且你使用牌造成伤害翻倍（此技能只对人机有效）",

  ["@wjsl_zhenta_num"] = "计数",

  ["pdlboxreset"] = "魔盒：技能重置",

  ["author_says"] = "作者吟唱",
  ["dfst_introduce"] = "你觉得这个游戏模式玩起来怎么样？",
  ["justsoso"] = "感觉一般，凑合着玩",
  ["justlike"] = "卧槽！可以啊。点个赞！！！",
  ["justbored"] = "算了吧，下次不玩了~",

  ["#skills-choice"] = "随机选项选择",
  ["#relife-choice"] = "重生玩家请选择",
  ["#rstskills_1"] = "重置魔盒技能（需要5金币）",
  ["#rstskills_2"] = "重置魔盒技能（需要7金币）",
  ["#rstskills_3"] = "重置魔盒技能（需要10金币）",
  ["#rstskills_4"] = "重置魔盒技能（需要12金币）",
  ["#rstskills_5"] = "重置魔盒技能（需要15金币）",
  ["#getBossSkills-choice"] = "获取神将技能",
  ["#upAI-choice"] = "选择你要升级的AI队友",
  ["time limitation: 10s"] = "游戏长达10秒",
  ["1v1: left you alive"] = "任何时候，任何人数都可以投降",

  ["gaingold"] = "获得金币",
  [":gaingold"] = "获得金币随机40~70，层数越高随机金币下限有一定提升",
  ["maxhp"] = "成长",
  [":maxhp"] = "随机增加体力与最大体力值",
  ["extraslash_num"] = "连斩",
  [":extraslash_num"] = "你获得随机2-4随机【杀数+】标记，此标记能够在每个阶段使用杀次数额外增加对应标记值",
  ["angelbless"] = "天使祝福",
  [":angelbless"] = "天使祝福让你摸体力上限的手牌，回满体力，额外获得一层标记增益或者些许金币",
  ["magicbox"] = "魔盒的交易",
  [":magicbox"] = "你获得随机1-3【魔盒】标记（当获得该标记时，获得等量的随机技能。每到选项奖励可以用金币重置【魔盒】标记技能）",
  ["slash_dmg_sel"] = "【振奋】强化",
  [":slash_dmg_sel"] = "【振奋】强化【杀】，【杀】指向目标伤害+1",
  ["trick_dmg_sel"] = "【振奋】强化",
  [":trick_dmg_sel"] = "【振奋】强化【伤害锦囊】，【伤害锦囊】指向目标伤害+1",
  ["lingxin"] = "凌心",
  [":lingxin"] = "你获得随机1-3【凌心】标记，此标记能够使你的摸牌阶段摸牌数和手牌上限额外增加该标记值",
  ["shieldnum"] = "护盾",
  [":shieldnum"] = "你获得随机1-2随机【护甲+】标记，此标记能够在每回合开始把护甲叠到对应标记值",
  ["relife"] = "重生",
  [":relife"] = "你可以选择一名死亡玩家复活",
  ["guard_angel"] = "守护天使",
  [":guard_angel"] = "你活得“守护天使”标记，当你死亡时候可以满血复活并摸等量的牌，结束当前玩家回合阶段",
  ["rsetrwd_1"] = "重置奖励",
  [":rsetrwd_1"] = "可以花费20金币重置一次奖励",
  ["rsetrwd_2"] = "重置奖励",
  [":rsetrwd_2"] = "可以花费30金币重置一次奖励",
  ["rsetrwd_3"] = "重置奖励",
  [":rsetrwd_3"] = "可以花费40金币重置一次奖励",
  ["upAI"] = "升级人机",
  [":upAI"] = "升级你的人机队友，其最大体力与获得随机技能数+1，随机获得一些标记+1",

  ["@@guardangle"] = "守护天使",
  ["@slash_dmg"] = "杀伤+",
  ["@trick_dmg"] = "锦伤+",
  ["@slash_num"] = "杀数+",
  ["@pdlbox"] = "魔盒+",
  ["@df_lingxin"] = "凌心+",
  ["@shieldnum"] = "护甲+",
  ["@gold_num"] = "金币:",

  ["pyshop_buyitems"] = "购买装备",
  ["pyshop_cellskills"] = "出售技能",
  ["#puyuanShop-choice"] = "浦元铁匠铺",
  ["#puyuanShop-buyeuips"] = "购买装备药水",
  ["#puyuanShop-cellskills"] = "出售技能",
  ["Exist"] = "离开",

  ["wjsl_zhenfen"] = "振奋",
  [":wjsl_zhenfen"] = "锁定技，【杀】、【伤害锦囊】指定目标的伤害值+获得强化的标记值，【护甲】、【凌心】每回合获得护甲和摸牌数也依此类推",

  ["wjsl_tips"] = "新手提示",
  ["wjsl_title"] = "新手提示",
  [":wjsl_title"] = "1.塔层共60层，难度逐层递增。下一层复活的层主摸牌数是根据玩家手牌、连胜次数来计算。<br/>2.玩家连胜5次将终结其阶段(超过40层改为4)，如果是多人玩家层主还会获得一个额外的回合。<br/>3.请不要摸牌过多以至于牌堆为0平局。",
  ["wjsl_title1"] = "浦沅商店",
  [":wjsl_title1"] = "层数大于10有概率遇到，可以买药水、稀有装备、技能。也可以卖技能（潘多拉技能除外）",
  [":wjsl_mode"] = desc_wjsl_mode,
}

return wjsl_mode
